<?php if (!defined('FIT_PATH')) exit('No direct script access allowed');
/**
 * @copyright 2009 - 2011 Bincent.com. All Rights Reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @package	FitPHP
 * @subpackage library
 * 
 * @author Hongbin Hsu <hongbin.hsu@gmail.com> Initial.
 * @version $Id: library/pagination.php 1.0 2010-09-18 16:52 hongbin.hsu $
 */
 
// ------------------------------------------------------------------------

class Library_Pagination
{
	protected $offset = null;           //当前页
	protected $total_rows;              //总的记录数
	protected $roll = 5;               //分栏数
	protected $per_page = 10;           //每页显示记录数
	protected $base_url = '#';          //分页跳转的参数
	//protected $segment = 'page';      //分页方法 URI 的哪个部分包含页数
	protected $first_link = 'First';    //自定义起始链接名字
	protected $prev_link = '&lt;';      //自定义上一页链接名字
	protected $next_link = '&gt;';      //自定义下一页链接名字
	protected $last_link = 'Last';      //自定义结束链接名字
	
	private $start;                     //开始页
	private $end;                       //结束页
	
	//----------------------------------------------------------
	/**
	 * 构造函数
	 *
	 * @param array $params
	 */
	public function __construct($params = array()){
        if (!empty($params)) {
        	$this->initialize($params);
        }
	}
	
	//----------------------------------------------------------
	/**
	 * 配置分页类基本参数 
	 *
	 * @param array $params
	 */
	public function initialize($params = array()){
        foreach ($params as $key => $value){
            $key = strtolower($key);
            $this->$key = $value;
        }
        
        $this->total_rows = ceil($this->total_rows/$this->per_page);
		
		/**
		if (!empty($_GET[$this->segment])) {
			$this->offset = $_GET[$this->segment];    //取得当前页数
		}
		**/
		$this->roll = ($this->roll > 1) ? $this->roll : 1;
		$this->offset = empty($this->offset) ? 1 : $this->offset;
		
		if($this->offset > $this->total_rows){
			$this->offset = $this->total_rows;             //屏蔽人为的输入超过页数的值
		}
		if($this->offset < 1){
			$this->offset = 1;                                //屏蔽人为的输入小于最小页数的值
		}
		$this->start = ($this->offset > 3) ? ($this->offset - 2) : 1; //避免从负数开始
		$this->end = $this->start + $this->roll - 1;
		
		if($this->total_rows <= $this->end){         //判断超过最大页数
			$this->start = $this->total_rows - $this->roll;
			$this->end = $this->total_rows;
		}
		if($this->total_rows <= $this->roll){         //总页数小于显示页数
		        $this->start = 1;
		        $this->end = $this->total_rows;
		}
		
		$this->base_url = rtrim($this->base_url, '/');
	}
        
	//----------------------------------------------------------
	/**
	 * 分页显示输出
	 *
	 */
	public function create_links(){
		$str = '';
		if ($this->offset > 1) {
			$str .= '<a href="'.$this->base_url.'1">'.$this->first_link.'</a>';
			$str .=  '<a href="'.$this->base_url.($this->offset-1).'">'.$this->prev_link.'</a>';
		}
		for($i = $this->start; $i <= $this->end; $i++) {
	        if ($i == $this->offset) {
	                $str .=  '<span>'.$i.'</span>';
	        }else{
	                $str .=  '<a href="'.$this->base_url.$i.'">'.$i.'</a>';
	        }
		}
		if ($this->offset < $this->total_rows) {
	        $str .=  '<a href="'.$this->base_url.($this->offset+1).'">'.$this->next_link.'</a>';
	        $str .=  '<a href="'.$this->base_url.$this->total_rows.'">'.$this->last_link.'</a>';
		}
		
		return $str;
	}
	
	
	//----------------------------------------------------------
	/**
	 * 分页AJAX显示输出
	 *
	 */
	public function create_ajax($func = ''){
		$str = '';
		if (empty($func)) { return $str; }
		if ($this->offset > 1) {
			$str .= '<a href="javascript:void(0);" onclick="'.$func.'(\''.$this->base_url.'1\')">'.$this->first_link.'</a>';
			$str .=  '<a href="javascript:void(0);" onclick="'.$func.'(\''.$this->base_url.($this->offset-1).'\')">'.$this->prev_link.'</a>';
		}
		for($i = $this->start; $i <= $this->end; $i++) {
	        if ($i == $this->offset) {
	                $str .=  '<span>'.$i.'</span>';
	        }else{
	                $str .=  '<a href="javascript:void(0);" onclick="'.$func.'(\''.$this->base_url.$i.'\')">'.$i.'</a>';
	        }
		}
		if ($this->offset < $this->total_rows) {
	        $str .=  '<a href="javascript:void(0);" onclick="'.$func.'(\''.$this->base_url.($this->offset+1).'\')">'.$this->next_link.'</a>';
	        $str .=  '<a href="javascript:void(0);" onclick="'.$func.'(\''.$this->base_url.$this->total_rows.'\')">'.$this->last_link.'</a>';
		}
		
		return $str;
	}
}